package com.party.admin.web.controller.log;

import com.google.common.collect.Maps;
import com.party.admin.biz.log.LogEventBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.paging.Page;
import com.party.common.utils.DateUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.log.BehaviorType;
import com.party.core.model.log.LogEvent;
import com.party.core.model.log.LogEventQuery;
import com.party.core.model.log.LogType;
import com.party.core.model.system.TargetType;
import com.party.core.service.log.ILogEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日志控制器
 * Created by wei.li
 *
 * @date 2017/9/5 0005
 * @time 18:33
 */

@Controller
@RequestMapping(value = "log/event")
public class LogEventController {

    @Autowired
    private ILogEventService logEventService;

    @Autowired
    private LogEventBizService logEventBizService;

    /**
     * 系统日志列表
     * @param logEventQuery 日志
     * @param page 分页参数
     * @return 日志列表
     */
    @RequestMapping(value = "system/list")
    public ModelAndView systemList(LogEventQuery logEventQuery, Page page, String projectType){
        ModelAndView modelAndView = new ModelAndView("log/systemLog");
        logEventQuery.setType(LogType.SYSTEM_LOG.getCode());
        page.setLimit(20);
        if (StringUtils.isNotEmpty(projectType)) {
            logEventQuery.setCallerClass("com.party." + projectType);
            modelAndView.addObject("projectType", projectType);
        }
        List<LogEvent> list = logEventBizService.list(logEventQuery, page);
        if (StringUtils.isNotEmpty(projectType)) {
            logEventQuery.setCallerClass(null);
        }
        modelAndView.addObject("page", page);
        modelAndView.addObject("list", list);
        return modelAndView;
    }


    /**
     * 行为日志列表
     * @param logEventQuery 日志
     * @param page 分页参数
     * @return 日志列表
     */
    @RequestMapping(value = "behavior/list")
    public ModelAndView behaviorList(LogEventQuery logEventQuery, Page page){
        ModelAndView modelAndView = new ModelAndView("log/behaviorLog");
        logEventQuery.setType(LogType.BEHAVIOR_LOG.getCode());
        page.setLimit(20);
        List<LogEvent> list = logEventBizService.list(logEventQuery, page);
        for (LogEvent logEvent : list) {
            if (StringUtils.isNotEmpty(logEvent.getBehaviorType())) {
                logEvent.setBehaviorType(BehaviorType.getValue(logEvent.getBehaviorType()));
            }
        }
        HashMap<String, String> behaviorType = BehaviorType.convertMap();
        Map<String, String> targetType = Maps.newHashMap();
        targetType.put(TargetType.ZC_ACTIVITY.getCode(), TargetType.ZC_ACTIVITY.getValue());
        modelAndView.addObject("behaviorType", behaviorType);
        modelAndView.addObject("targetType", targetType);
        modelAndView.addObject("page", page);
        modelAndView.addObject("list", list);
        return modelAndView;
    }


    /**
     * 行为列表
     * @return 行为时间
     */
    @RequestMapping(value = "behaviorList")
    public ModelAndView behaviorList(String operatorId){
        ModelAndView modelAndView = new ModelAndView("log/behaviorList");
        modelAndView.addObject("operatorId", operatorId);
        return modelAndView;
    }

    /**
     * 行为数据
     * @param operatorId 操作者
     * @param date 时间
     * @param page 分页参数
     * @return 行为时间
     */
    @ResponseBody
    @RequestMapping(value = "behaviorData")
    public AjaxResult behaviorData(String operatorId, String date, Page page){
        page.setLimit(10);
        AjaxResult ajaxResult = new AjaxResult();
        date = null == date ? DateUtils.todayDate() : date;
        List<LogEvent> list = logEventService.behaviorList(operatorId, date, page);
        Map<String, Object> map = Maps.newHashMap();

        map.put("logList", list);
        map.put("date", date);
        ajaxResult.setPage(page);
        ajaxResult.setData(map);
        return ajaxResult;
    }

    /**
     * 日志统计
     * @param operatorId 操作者
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "chart")
    public AjaxResult chart(String operatorId){
        AjaxResult ajaxResult = new AjaxResult();
        List<HashMap<String, Object>> list = logEventService.countByDate(operatorId);
        List<String> dateList = logEventBizService.dateStringList(15);
        List<Integer> chartList = logEventBizService.chartList(list, dateList);
        Map<String, List> map = Maps.newHashMap();
        map.put("dateList", dateList);
        map.put("chartList", chartList);
        ajaxResult.setSuccess(true);
        ajaxResult.setData(map);
        return ajaxResult;
    }

    /**
     * 日志视图
     * @param id 编号
     * @return 交互数据
     */
    @RequestMapping(value = "view")
    public ModelAndView view(String id){
        ModelAndView modelAndView = new ModelAndView("/log/logView");
        LogEvent logEvent = logEventService.get(id);
        modelAndView.addObject("logEvent", logEvent);
        return modelAndView;
    }
}
